// pages/user/login/login.js

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      showTopTips: false,
      info:'',
      isback:false,
      login_num:0,
      show_register:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
          title: '登录中',
      })
      
      this.platfromLogin();
      
  },

  /**
    * 生命周期函数--监听页面隐藏
    */
  onHide: function () {
      this.data.isback = true;
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      this.data.isback = false;
  },

  onShow:function(){
    app.globalData.login_num++;
    if(this.data.isback){
        wx.showLoading({
            title: '请稍等...',
        })
        setTimeout(function () {
            wx.hideLoading();
            wx.navigateBack()
        }, 500);
    }
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg=='getPhoneNumber:ok'){
      var data = { iv: e.detail.iv, encryptedData: e.detail.encryptedData, userinfo: app.globalData.userInfo}
      app.ajax({
        data: data,
        method:'POST',
        url: app.host + 'mall/Login/register',
        success: function (d) {
          if (d.data.code>0){
            wx.showLoading({
              title: '注册成功，请稍等...',
            })
            that.data.login_num = 0;
            that.platfromLogin();
          }else{
            app.showTopTips('注册失败', that);
          }
        }
      });
    }else{
      app.showTopTips('注册失败', that);
    }
  },
  wxLogin: function (auto) {
      var that = this;
      wx.login({
          success: function (res) {
              if (res.code) {
                  //发起网络请求
                  app.ajax({
                      url: app.host + 'mall/Login/wxLogin',
                      data: {
                          code: res.code
                      },
                      success: function (d) {
                          if (auto == true) {
                              that.platfromLogin();
                          }
                      }
                  })
              } else {
                  console.log('获取用户登录态失败！' + res.errMsg)
              }
          }
      });
  },

  platfromLogin: function () {
      var that = this;
      that.data.login_num++;
      if (that.data.login_num>5){
          app.showTopTips('登录失败', that);
          return false;
      }
      var data = {userinfo: app.globalData.userInfo };
      app.ajax({
        data: data,
        method: 'POST',
        url: app.host + 'mall/Login/index',
        success: function (d) {
          wx.hideLoading();
            var da = d.data;
            if (da.code <= 0) {
                if (da.data == 'wxlogin') { //没有openid 调用微信登录
                    that.wxLogin(true);
                } else if (da.data == 'register') { //没有注册 跳转到注册页面
                    // wx.navigateTo({
                    //     url: '/pages/usercenter/register/register'
                    // })
                  that.setData({
                    show_register:true
                  });
                } else if (da.data == 'disable') { //账号被禁用 页面后退
                  setTimeout(function () {
                    wx.hideLoading();
                    wx.navigateBack()
                  }, 500);
                } else { //页面后退
                  setTimeout(function () {
                    wx.hideLoading();
                    wx.navigateBack()
                  }, 500);
                }
            }else{
                app.globalData.is_login=true;
                setTimeout(function () {
                  wx.hideLoading();
                  wx.navigateBack()
                }, 500);
            }
        }
      })
  },
})